package com.home.moon.base.filter;

import com.auth0.jwt.JWT;
import com.home.moon.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.rmi.RemoteException;

/**
 * @author auto
 */
@WebFilter(urlPatterns = {"/user/**"})
@Component
public class LoginFilter implements Filter {
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try{
            String jwt = (String)servletRequest.getAttribute("token");
            Object value = redisUtil.get(jwt);
            if(value == null){
                throw new RemoteException();
            }
            JWT.decode(jwt);
            filterChain.doFilter(servletRequest,servletResponse);
        }catch (Exception e){
            throw new RuntimeException("请检查登录状态");
        }
    }
}
